package com.ibm.law.ui.system.setup;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import com.ibm.law.data.CachedData;
import com.ibm.law.domain.Institute;
import com.ibm.law.service.system.InstituteService;
import com.ibm.law.ui.AppWindow;

public class LisenceImageDialog extends Dialog {
	private DataBindingContext m_bindingContext;

	protected Object result;
	protected Shell shell;
	private Image image;
	private Button btnNewButton;
	private Institute institute = CachedData.INSTITUTE;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public LisenceImageDialog(Shell parent, int style) {
		super(parent, style);
		setText("许可证");
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(800, 600);
		shell.setText(getText());
		shell.setLayout(new GridLayout(5, false));

		btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		if (institute.getLisencePath() != null) {
			btnNewButton.setImage(new Image(AppWindow.getInstance().getShell()
					.getDisplay(), institute.getLisencePath()));
		} else {
			btnNewButton.setEnabled(false);
		}
		GridData gd_btnNewButton = new GridData(SWT.FILL, SWT.FILL, true, true,
				5, 1);
		gd_btnNewButton.widthHint = 438;
		btnNewButton.setLayoutData(gd_btnNewButton);
		new Label(shell, SWT.NONE);

		Button btnNewButton_1 = new Button(shell, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog dialog = new FileDialog(AppWindow.getInstance()
						.getShell());
				String file = dialog.open();
				File src = new File(file);
				String imgPath = "D:/tmp/" + src.getName();
				institute.setLisencePath(imgPath);
				try {
					FileUtils.copyFile(new File(file), new File(imgPath));
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				image = new Image(AppWindow.getInstance().getShell()
						.getDisplay(), imgPath);
				btnNewButton.setEnabled(true);
				btnNewButton.setImage(image);
			}
		});
		btnNewButton_1.setText("导入");

		Button btnNewButton_2 = new Button(shell, SWT.NONE);
		btnNewButton_2.setText("导出到word");

		Button btnNewButton_3 = new Button(shell, SWT.NONE);
		btnNewButton_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				InstituteService instituteService=new InstituteService();
				instituteService.update(institute);
			}
		});
		btnNewButton_3.setText("保存");

		Button btnNewButton_4 = new Button(shell, SWT.NONE);
		btnNewButton_4.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				 if (e.getSource() instanceof Button) {
						Button button = (Button) e.getSource();
						button.getShell().close();
						
					}
			}
		});
		btnNewButton_4.setText("退出");
		m_bindingContext = initDataBindings();

	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		return bindingContext;
	}
}
